<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="ISO-8859-1"%>
<%@ page import = "quiz.Account" %>
<%@ page import = "java.util.ArrayList" %>
<jsp:include page="helpers/security.jsp" flush = "true" />
<%@ page import = "quiz.Message" %>
<%@ page import = "quiz.Achievement" %>
<%@ page import = "quiz.Result" %>
<%@ page import = "quiz.Quiz" %>
<%
	Account myAcct = (Account)session.getAttribute("account");
	String possesiveName = myAcct.getName();
	if (possesiveName.charAt(possesiveName.length() - 1) == 's')
		possesiveName += "'";
	else
		possesiveName += "'s";
	possesiveName += " profile";
	
	ArrayList<Message> announcements = (ArrayList<Message>)request.getAttribute("announcements");
	ArrayList<Message> messages = (ArrayList<Message>)request.getAttribute("messages");
	ArrayList<Achievement> achievements = (ArrayList<Achievement>)request.getAttribute("achievements");
	ArrayList<Result> activity = (ArrayList<Result>)request.getAttribute("recentResults");
	ArrayList<Result> friendActivity = (ArrayList<Result>)request.getAttribute("friendsRecentResults");
	ArrayList<Quiz> recentQuizzes = (ArrayList<Quiz>)request.getAttribute("recentQuizzesCreated");
	ArrayList<Quiz> popularQuizzes = (ArrayList<Quiz>)request.getAttribute("popularQuizzes");
	
%>  
<jsp:include page="templates/header.jsp" flush="true">
	<jsp:param value = "<%=possesiveName%>" name = "title" />
</jsp:include>
<jsp:include page="templates/title_bar.jsp" flush = "true">
	<jsp:param value = "<%=possesiveName%>" name = "title" />
</jsp:include>
<div id = "content">
<div class = "center">
<div id = "left_col">
<div id = "announcements"><div class = "section_title border_bottom">Announcements</div>
<%
	if (announcements.size() == 0) out.println("No new announcements. Enjoy your day :)");
	for(Message m: announcements) {
		out.println("<div>"+m.getContent()+" || <a href = \"ViewMessage?messageID="+m.getID()+"\">View</a>");
		if(myAcct.isAdmin()) out.println(" || <a href = \"DeleteAnnounce?announceID=" +m.getID()+"\">Delete</a>");
		out.println("</div>");
	}
%>
</div>
<div id = "activity" class = "profile"><div class = "section_title">Recent Activity</div>
<%
	if (activity == null || activity.size() == 0) out.println("No recent activity! You should take a quiz!");
	for (Result r: activity) {
		out.println("<div id = \"result\">You scored "+r.getScore()+" on <a href = \"ShowSelectedQuiz?id="+r.getQuiz().getID()+"\">"+r.getQuiz().getName()+"</a></div>");
	}
%>
<a href = "DisplayHistory">View all</a>
</div>
<div id = "take_quizzes" class = "profile">
<div class = "section_title">Take a Quiz</div>
<div id = "take_quizzes_left"><div class = "section_title_small">Recently Added Quizzes</div>
<%	
	if (recentQuizzes.size() == 0) out.println("No recently added quizzes");
	for (Quiz q: recentQuizzes) {
		out.println("<div id = \"quiz\"><a href = \"ShowSelectedQuiz?id="+q.getID()+"\">"+q.getName()+"</a></div>");
	}
%>
</div>
<div id = "take_quizzes_right"><div class = "section_title_small">Popular Quizzes</div>
<%
	if (popularQuizzes.size() == 0) out.println("No popular quizzes");
	for (Quiz q: popularQuizzes) {
		out.println("<div id = \"quiz\"><a href = \"ShowSelectedQuiz?id="+q.getID()+"\">"+q.getName()+"</a></div>");
	}
%>
</div>
<br style = "clear: both;" />
</div>
</div><div id = "right_col">
<span class = "btn"><a href = "create-quiz.jsp">+ Create New Quiz</a></span>
<%
	if (myAcct.isAdmin()) out.println("<a href = \"AdminPage\"><span class = \"btn\">View Admin Portal</span></a>");
%>
<div id = "messages" class = "profile bottom_border"><div class = "section_title_small border_bottom">Messages (<a href = "SendMessage">Compose</a>)</div>
<%
	if (messages.size() == 0) out.println("No messages!");
	for (Message m: messages) {
		String content = m.getContent();
		if (content.length() > 30) content = content.substring(0, 29) + "...";
		else if (content.length() == 0) content = "[NO BODY]";
		if (m.getType() == Message.REQUEST) content = "Friend request";
		out.println("<div>");
		if (!m.has_been_read()) out.print("<b>");
		out.println(content);
		if (!m.has_been_read()) out.print("</b>");
		out.println("<a href = \"ViewMessage?messageID="+m.getID()+"\">View</a></div>");
	}
%>
</div>
<div id = "achievements" class = "profile bottom_border"><div class = "section_title_small border_bottom">Achievements</div>
<%
	if (achievements.size() == 0) out.println("You&#39;ve achieved nothing");
	for (Achievement a: achievements) {
		out.println("<div id = \"achievement\" class = \"padding\">"+Achievement.getAchievementName(a)+"</div>");
	}
%>
</div>
<div id = "friends" class = "profile"><div class = "section_title_small border_bottom">Friends</div>
<%
	if (friendActivity.size() == 0) out.println("Your friends did nothing");
	for (Result r: friendActivity) {
		out.println("<div id = \"result\"><a href = \"Profile?id="+r.getUser().getID()+"\">"+r.getUser().getName()+"</a> scored "+r.getScore()+" on <a href = \"ShowSelectedQuiz?id="+r.getQuiz().getID()+"\">"+r.getQuiz().getName()+"</a></div>");
	}
%>
</div>
<a href = "LogOut">Log Out</a>
</div>
<br style = "clear: both;" />
</div>
</div>
<jsp:include page="templates/footer.jsp" flush="true" />